package com.movisol.adsservice.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.util.Log;

public class BannerTracker {
public void trackImpression(String url,int bsid){
	
	asyncGet(formURL(url,String.valueOf(bsid)));
}



public void trackClick(String url,int bsid){
	
	asyncGet(formURL(url,String.valueOf(bsid)));
}
private void asyncGet(String url){
	
	class AsyncTrack extends AsyncTask<URL, Void, Void>
	{

		@Override
		protected Void doInBackground(URL... urlparam) {
		
				//Method 1
				/*
				HttpURLConnection c;
				try {
					c = (HttpURLConnection) urlparam[0].openConnection();
					
					// do some setup
					c.setDoInput(true); 
					c.setDoOutput(true); 
					c.setUseCaches(false); 
					c.setRequestMethod("GET"); 

					// connect and flush the request out
				
					c.connect();
					c.getOutputStream().flush();
				
					c.getInputStream();
				
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			*/
			 //Method 2
			 
				HttpClient client = new DefaultHttpClient();
				HttpGet request = new HttpGet();
				BufferedReader in = null;
				try {
					request.setURI(urlparam[0].toURI());
					HttpResponse response = client.execute(request);
					in = new BufferedReader(new InputStreamReader(response
							.getEntity().getContent()), 8192);
					StringBuffer sb = new StringBuffer("");
					String line = "";
					String NL = System.getProperty("line.separator");
					while ((line = in.readLine()) != null) {
						sb.append(line + NL);
					}
					in.close();
					String page = sb.toString();
					Log.d("BannerTracker", page);
				
				} catch (URISyntaxException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					if (in != null) {
						try {
							in.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
	           


			
			return null;
		
		}

	}
	
	URL destUrl;
	try {
		destUrl = new URL(url);
		new AsyncTrack().execute(destUrl);	
	} catch (MalformedURLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}
private String formURL (String url,String replace){
	
	String formedUrl=url.replaceAll("\\[\\[bsid\\]\\]",replace);
	return formedUrl;
}
}